package com.jungle.admin.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jungle.common.annotation.RepeatSubmit;
import com.jungle.common.constant.MessageConstant;
import com.jungle.common.core.controller.BaseController;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.domain.R;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.core.validate.AddGroup;
import com.jungle.common.core.validate.EditGroup;
import com.jungle.common.exception.ServiceException;
import com.jungle.pojo.domain.dto.ImgLinkDataDTO;
import com.jungle.pojo.domain.vo.SysOssResult;
import com.jungle.system.application.domain.bo.SysOssBo;
import com.jungle.system.application.service.ISysOssService;
import com.jungle.system.config.MinioConfig;
import com.jungle.system.utils.oss.AliOssUtil;
import com.jungle.system.utils.oss.MinioUploadUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;


/**
 * 文件操作
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/oss")
public class SysOssController extends BaseController {

    private final ISysOssService service;
    private final AliOssUtil aliOssUtil;


    /**
     * 文件上传
     * @param file
     */
    @PostMapping(value = "/upload1")//,consumes = MediaType.MULTIPART_FORM_DATA_VALUE
    public void upload1(@RequestPart("file") MultipartFile file){
        log.info("文件上传");
        if (ObjectUtil.isNull(file)){
            throw new ServiceException(MessageConstant.FILE_IS_NULL);
        }
        SysOssResult ossResult=service.upload(file);
    }




    /**
     * 文件上传阿里云OSS
     *
     * @param file
     */
    @PostMapping(value = "/uploadAliOss")
    public R<String> uploadAliOss(@RequestPart("file") MultipartFile file)  {
        log.info("文件上传");
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException(MessageConstant.FILE_IS_NULL);
        }
        try {
            String originalFilename = file.getOriginalFilename();
//            String fileName = imgDTO.getUseTo()+"-"+IdUtil.randomUUID()
//                    + originalFilename.substring(originalFilename.lastIndexOf("."));
            String url = aliOssUtil.upload(file.getBytes(), originalFilename);
            return R.ok(url);
        } catch (IOException e) {
            log.info("上传失败");
        }
        return R.ok(MessageConstant.UPLOAD_FAILED);
    }


    /**
     * 文件上传至Minio
     * @param file 文件
     */
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Object> upload(@RequestPart("file") MultipartFile file){
        log.info("文件上传");
        if (ObjectUtil.isNull(file)){
            throw new ServiceException(MessageConstant.FILE_IS_NULL);
        }
        SysOssResult ossResult = service.upload(file);

        if (null == ossResult) {
            return R.fail("上传失败");
        }
        return R.ok(ossResult);
    }



    /**
     * 查询OSS对象存储对象列表
     */
    @GetMapping("/list")
    public TableDataInfo<com.jungle.domain.vo.SysOssVo> list(SysOssBo bo, PageQuery pageQuery) {
        return service.queryPageList(bo, pageQuery);
    }
    /**
     * 导入OSS对象存储对象列表
     */
    /**
     * 导出OSS对象存储对象列表
     */

    /**
     * 获取OSS对象存储对象详细信息
     *
     * @param ossId 主键
     */
    @GetMapping("/{ossId}")
    public R<com.jungle.domain.vo.SysOssVo> getInfo(@NotNull(message = "主键不能为空")
                                                    @PathVariable Long ossId) {
        return R.ok(service.queryById(ossId));
    }

    /**
     * 新增OSS对象存储对象
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOssBo bo) {
        return toAjax(service.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改OSS对象存储对象
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOssBo bo) {
        return toAjax(service.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除OSS对象存储对象
     *
     * @param ossIds 主键串
     */
    @DeleteMapping("/{ossIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ossIds) {
        return toAjax(service.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0);
    }


}
